package com.iscale.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.iscale.model.entity.BaseRegionInfo;
import com.iscale.model.entity.SysDictionary;
import com.iscale.mapper.SysDictionaryMapper;
import com.iscale.service.SysDictionaryService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.iscale.util.ResponseCode;
import com.iscale.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典 服务实现类
 * </p>
 *
 * @author Andychen
 * @since 2017-03-08
 */
@Service
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryMapper, SysDictionary> implements SysDictionaryService {

    @Autowired
    private RedisTemplate<String, List<SysDictionary>> redisTemplate;

    @Override
    public ResponseCode saveOrUpdateDictionary(SysDictionary sysDictionary){
        if(null==sysDictionary.getId() || sysDictionary.getId()==0){
            if(StringUtil.isNotEmpty(sysDictionary.getKeyName())){
                //从数据查询
                EntityWrapper<SysDictionary> entityWrapper = new EntityWrapper<>(new SysDictionary());
                entityWrapper.where("keyName={0}",sysDictionary.getKeyName());
                int exit = selectCount(entityWrapper);
                if(exit>0){
                    //key 已经存在，不允许重复
                    return  ResponseCode.key_name_exist;
                }
            }
        }
        boolean result = insertOrUpdate(sysDictionary);
        if(!result)return ResponseCode.add_director_error;
        return null;
    }

    @Override
    public String getSysDictionaryByKey(String key){
        String keyValu = null;
        //从缓存中读取
        List<SysDictionary> lists = redisTemplate.opsForValue().get("system-dictionary");
        if (lists != null && lists.size()>0) {
            for (SysDictionary sysDictionary:
            lists ) {
                if(StringUtil.isNotEmpty(sysDictionary.getKeyName()) && sysDictionary.getKeyName().equals(key.trim())){
                    keyValu =  sysDictionary.getKeyValue();
                    break;
                }
            }
        }
        if(null!=keyValu){
            return keyValu;
        }
        //从数据查询
        EntityWrapper<SysDictionary> entityWrapper = new EntityWrapper<>(new SysDictionary());
        entityWrapper.where("keyName={0}",key);
        List<SysDictionary> sysDictionaries = selectList(entityWrapper);
        if(null!=sysDictionaries && sysDictionaries.size()>0){
            return sysDictionaries.get(0).getKeyValue();
        }
        return keyValu;
    }


    @Override
    public List<SysDictionary> selectDictionary(Long pid,String keyName){
        //从数据查询
        EntityWrapper<SysDictionary> entityWrapper = new EntityWrapper<>(new SysDictionary());
        entityWrapper.where("1={0}",1);
        if(null!=pid){
            entityWrapper.andNew("pid={0}",pid);
        }
        if(StringUtil.isNotEmpty(keyName)){
            entityWrapper.like("keyName",keyName);
        }
        List<SysDictionary> sysDictionaries = selectList(entityWrapper);

        return sysDictionaries;
    }


    @Override
    public List<SysDictionary> selectDictionaryGroup(){
        //从数据查询
        EntityWrapper<SysDictionary> entityWrapper = new EntityWrapper<>(new SysDictionary());
        entityWrapper.where("status={0}",0);
        entityWrapper.andNew("pid is null");
        entityWrapper.or("pid={0}",0);
        List<SysDictionary> sysDictionaries = selectList(entityWrapper);
        return sysDictionaries;
    }
}
